//
//  NowView.swift
//  MovieAppDemo
//
//  Created by Demo on 2025/1/25.
//

import SwiftUI

struct NowView: View {
    @State private var selectedTab: String = "clock"
    @State private var imageLoading: Bool = false
    @State private var scrollOffset: CGFloat = 0
    @State private var showBackdrop: Bool = true
    @StateObject private var viewModel = MainPicaViewModel()
    @State private var currentPage: Int = 1
    @State private var fav_type: Int = 2
    @State private var isLoading = false // 用于防止重复加载
    @State private var hasAppeared = false
    
    
    var body: some View {
        ScrollViewReader { scrollViewProxy in // 使用ScrollViewReader来控制滚动
            ZStack(alignment: .top) {
                ScrollView {
                    GeometryReader { geometry in
                        Color.clear
                            .onChange(of: geometry.frame(in: .global).minY) { oldValue, value in
                                scrollOffset = value
                            }
                    }
                    .frame(height: 0)
                    
                    LazyVGrid(
                        columns: [GridItem(.adaptive(minimum: 300))], // 每个卡片的最小宽度为 160，宽屏时每行可以显示多个
                        spacing: 20
                    ) {
                        ForEach(viewModel.lists, id: \.id) { item in
                            NavigationLink {
                                DetailView(item: item)
                            } label: {
                                NowCard(item: item)
                                    .frame(height: 400)
                                
                            }
                            .id(item.id)  // 确保 NavigationLink 使用唯一标识符
                        }
                        GeometryReader { geo in
                            Color.clear
                            .onChange(of: geo.frame(in: .global).maxY) { oldValue, maxY in
                                let scrollThreshold = UIScreen.main.bounds.height * 1.5
                                isLoading = viewModel.isLoading
                                if maxY < scrollThreshold, !isLoading {
                                    loadNextPage()
                                }
                            }
                        }
                        .frame(height: 1) // 确保不会占用空间
                    }
                    .onChange(of: selectedTab) { oldValue, newValue in
                        loadNew(scrollViewProxy:scrollViewProxy)
                    }
                    .onAppear {
                        if !hasAppeared {
                            // 执行首次加载的代码
                            loadNew(scrollViewProxy: scrollViewProxy)
                            hasAppeared = true
                        }
                    }
                    
                    
                    .padding(10)
                    .padding(.top, 50)
                }
                
                ZStack(alignment: .top) {
                    if showBackdrop {
                        BackdropBlurView(radius: 4)
                            .frame(height: 120)
                    }
                    NowViewTabs(selectedTab: $selectedTab,  fav_type: $fav_type)
                }

                .animation(.default, value: selectedTab)
                .ignoresSafeArea()
                
                
            }
            .background(Color("BgColor1"))
            
        }
    }
    
    private func loadNew(scrollViewProxy: ScrollViewProxy) {
        viewModel.get_list(type:0,page: 1,fav_type:fav_type)
        withAnimation {
            scrollViewProxy.scrollTo(viewModel.lists.first?.id, anchor: .top)
        }
    }
 
    private func loadNextPage() {
        isLoading = viewModel.isLoading
        if !isLoading {
            triggerHapticFeedback()
            print("加载下一页: 当前页数 \(currentPage)")
            currentPage += 1
            viewModel.get_list(type:0,page: currentPage,fav_type:fav_type)
            
        }
    }
}

#Preview {
    NowView()
}

